﻿/// <reference path="../Controls/BaseComplexValueControl.js" />
/// <reference path="TreeNodePosition.js" />

var $TreeNodeContainer = TA.Turbo.Controls.TreeNodeContainer = function TreeNodeContainer()
{
}

$TreeNodeContainer.prototype.get_ContainerElement = function get_ContainerElement()
{
}
$TreeNodeContainer.prototype.get_NodeCollection = function get_NodeCollection()
{
}
$TreeNodeContainer.prototype.get_Owner = function get_Owner()
{
}
$TreeNodeContainer.prototype.AddNode = function AddNode(node)
{
	this.get_NodeCollection().push(node);

	var previousNodeIndex = this.get_NodeCollection().length - 2;
	if (previousNodeIndex >= 0)
	{
		var previousNode = this.get_NodeCollection()[previousNodeIndex];
		previousNode.Position = this._GetNodePosition(previousNodeIndex);
		previousNode.UpdateDisplay();
	}

	node.Create(
		this.get_Owner(),
		this.get_ContainerElement(),
		null, 
		this._GetNodePosition(this.get_NodeCollection().length - 1));
}
$TreeNodeContainer.prototype.RemoveNode = function RemoveNode(node)
{
	node._RemoveElements();
	this.get_NodeCollection().Remove(node);
}
$TreeNodeContainer.prototype._ParseChildren = function _ParseChildren()
{
	var iNode = 0;
	var cn = this.get_ContainerElement().get_ChildNodes(), cnl = cn.length;
	for (var iElem = 0; iElem < cnl; ++iElem)
	{
		var elem = cn[iElem];

		if (elem.tagName == "DIV" && elem.HasClass("tv_node"))
		{
			var node = this.get_NodeCollection()[iNode];

			node.Parse(this.get_Owner(), elem);

			++iNode;
		}
	}

}
$TreeNodeContainer.prototype._CreateChildren = function _CreateChildren()
{
	var c = this.get_NodeCollection(), cl = c.length;
	for (var iChild = 0; iChild < cl; ++iChild)
	{
		var childNode = c[iChild];

		childNode.Create(this.get_Owner(), this.get_ContainerElement(), null, this._GetNodePosition(iChild));
	}
}
$TreeNodeContainer.prototype._GetNodePosition = function _GetNodePosition(iChild)
{
	var position = null;
	if (this.get_NodeCollection().length == 1)
	{
		position = this == this.get_Owner() ? TreeNodePosition.RootOnly : TreeNodePosition.SubOnly;
	}
	else if (iChild == 0)
	{
		position = this == this.get_Owner() ? TreeNodePosition.RootFirst : TreeNodePosition.SubFirst;
	}
	else if (iChild == this.get_NodeCollection().length - 1)
	{
		position = this == this.get_Owner() ? TreeNodePosition.RootLast : TreeNodePosition.SubLast;
	}
	else
	{
		position = this == this.get_Owner() ? TreeNodePosition.RootRegular : TreeNodePosition.SubRegular;
	}
	
	return position;
}